package com.cat.chapter039;

/**
 * @author cat
 * @description https://leetcode.cn/problems/decode-string/
 * @create 2025/7/9 18:57
 * @since JDK17
 */

public class Solution02 {
    int where;

    public String decodeString(String s) {
        return f(s.toCharArray(), 0);
    }

    public String f(char s[], int cur) {
        StringBuilder sb = new StringBuilder();
        int num = 0;
        while (cur < s.length && s[cur] != ']') {
            if (s[cur] >= '0' && s[cur] <= '9') {
                num = num * 10 + s[cur] - '0';
//                System.out.println(num);
            } else if (s[cur] == '[') { //
                sb.append(f(s, cur + 1).repeat(num == 0 ? 1 : num));
                cur = where;
                num = 0;
            } else {
                sb = sb.append(s[cur]); // 将其像后面加一个
            }
            cur++;
        }
        where = cur;
        return sb.toString();
    }

    public static void main(String[] args) {
        Solution02 solution02 = new Solution02();
        System.out.println(solution02.decodeString("3[a]2[bc]") + "0");
    }
}
